package com.chen.stopwatch.meizu.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.chen.stopwatch.meizu.R;
import com.chen.stopwatch.meizu.utils.Utils;

public class TimeBarView extends View {
	public TimeBarView(Context context) {
		super(context);
		_context = context;
		loadRes();
	}

	public TimeBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		_context = context;
		loadRes();
	}

	public TimeBarView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		_context = context;
		loadRes();
	}

	String[] _text = null;

	Context _context = null;

	Rect[] _rects = null;

	Paint _paint = null;

	public void setTextsRect(Rect[] rects, Paint paint) {
		_rects = rects;
		_paint = paint;
	}

	void loadRes() {
		_text = new String[4];
		_text[0] = _context.getString(R.string.time_view_hour);
		_text[1] = _context.getString(R.string.time_view_minute);
		_text[2] = _context.getString(R.string.time_view_second);
		_text[3] = _context.getString(R.string.time_view_millsecond);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (null == _rects || null == _paint || null == _text)
			return;

		int textHeight = Utils.getFontHeight(_paint);
		_paint.setTextSize(((getBottom() - getTop()) / textHeight)
				* _paint.getTextSize());
		int y = (int) ((getBottom() - getTop()) * 0.75);

		for (int i = 0; i < 4; i++) {
			if (null == _text[i])
				continue;

			int x = (int) (_rects[i].left + ((_rects[i].right - _rects[i].left) - _paint.measureText(_text[i])) / 2);
			canvas.drawText(_text[i], x, y, _paint);
		}
	}
}
